package com.jy.event.domain;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by hanxi on 2017/7/22.
 */
@Entity
@Data
@NoArgsConstructor
public class Payment {
    @Id
    @Column(length = 50)
    private String id;
    private String orderId;
    @Column(columnDefinition = "TIMESTAMP(3) NULL")
    private Date createTime;
    private String storeId;
    private Double amount;
    private boolean success = false;
    @Enumerated(EnumType.STRING)
    private PaymentChannel channel;
    //主要针对第三方支付
    @Enumerated(EnumType.STRING)
    private PaymentStatus status;

    //当支付方式实物优惠券时，以下属性不为空
    /**
     * 优惠券名称
     */
    private String couponName;
    /**
     * 抵用商品id
     */
    private String productId;
    /**
     * 抵用商品名称
     */
    private String productName;
    /**
     * 优惠券实例编码
     */
    private String couponInstanceCode;
    /**
     * 抵用商品销售价格
     */
    private Integer productSalePrice;
    /**
     * 抵用商品数量
     */
    private Short productQuantity;

    /**
     * 优惠券类型
     */
    @Enumerated(EnumType.STRING)
    private CouponType type;

    @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3)")
    private Date addTime;
    @Column(insertable = false, updatable = false, columnDefinition = "TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)")
    private Date updateTime;

    public Payment(String id, String orderId, Double amount, PaymentChannel channel, String storeId) {
        this.id = id;
        this.orderId = orderId;
        this.createTime = new Date();
        this.success = false;
        this.amount = amount;
        this.channel = channel;
        this.storeId = storeId;
        this.status = PaymentStatus.NOTSURE;
    }

    /**
     * 优惠卷支付数据初始化
     *
     * @param id
     * @param orderId
     * @param storeId
     * @param productId
     * @param productName
     * @param couponInstanceCode
     * @param productSalePrice
     * @param productQuantity
     */
    public Payment(String id, String orderId, double amount, String storeId,
                   String productId, String productName, String couponInstanceCode,
                   int productSalePrice, short productQuantity, String couponName,
                   CouponType type) {
        this.id = id;
        this.orderId = orderId;
        this.createTime = new Date();
        this.success = false;
        this.amount = amount;
        this.channel = PaymentChannel.COUPON;
        this.storeId = storeId;
        this.status = PaymentStatus.SUCCESS;
        this.productId = productId;
        this.productName = productName;
        this.couponInstanceCode = couponInstanceCode;
        this.productSalePrice = productSalePrice;
        this.productQuantity = productQuantity;
        this.couponName = couponName;
        this.type = type;
    }


    /**
     * 判断第三方支付是否支付是否需要查询
     *
     * @return
     */
    public boolean checkStausChannel() {
        //如果为第三方支付 切未支付成功 在支付中 时间 小于5分钟
        if ((PaymentChannel.ALIPAY == channel ||
                PaymentChannel.WECHATPAY == channel ||
                channel == PaymentChannel.PAYCARD)
                && !success && status == PaymentStatus.NOTSURE &&
                new Date().getTime() - addTime.getTime() < 1000 * 60 * 5) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否为第三方支付
     *
     * @return
     */
    public boolean checkChannel() {
        if (PaymentChannel.ALIPAY == channel ||
                PaymentChannel.WECHATPAY == channel || channel == PaymentChannel.PAYCARD) {
            return true;
        }
        return false;
    }

    /**
     * 判读是否为二维码支付（如果支付未成功）
     *
     * @return
     */
    public boolean checkChannelTrue() {
        if (PaymentChannel.ALIPAY == channel ||
                PaymentChannel.WECHATPAY == channel) {
            return true;
        }
        return false;
    }
}
